/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.bikerz.user.bike.dao;


import br.com.bikerz.user.bike.domain.UserBike;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

/**
 *
 * @author munhofi
 */
@Repository
public class UserBikeWithJPA implements UserBikeDAO {

 
        private final static Logger logger = Logger.getLogger(UserBike.class.getName());

	@PersistenceContext
	protected EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public List<UserBike> findByTitle(String nome) {
		Query query = entityManager.createQuery("Select m from UserBike m where m.nome LIKE :nome");
		query.setParameter("nome", nome + "%");

		return query.getResultList();
	}

	public UserBike loadById(Long id) {
		return entityManager.find(UserBike.class, id);
	}

	public void persist(UserBike userBike) {
		entityManager.persist(userBike);
	}

	public void update(UserBike userBike) {
		entityManager.merge(userBike);
	}

	public void delete(UserBike userBike) {
		//entityManager.remove(entityManager.merge(userBike));
		//entityManager.remove(userBike);
		entityManager.createQuery("DELETE FROM UserBike m WHERE m.id=:id").setParameter("id", userBike.getId()).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public List<UserBike> loadAll() {
		return entityManager.createQuery("Select m from UserBike m").getResultList();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

}
